//
//  ExchangeViewController.swift
//  textfield
//
//  Created by ap0stader on 2023/10/19.
//

import UIKit

class ExchangeViewController : UIViewController, UITextFieldDelegate {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    @IBAction func dismissKeyboard(_ sender: UITapGestureRecognizer) {
        RMBInput.becomeFirstResponder()
        RMBInput.resignFirstResponder()
    }
    
    @IBOutlet var RMBInput: UITextField!
    
    @IBOutlet var dollarShow: UILabel!
    
    @IBAction func RMBInputChange(_ sender: UITextField) {
        if let a = RMBInput.text, let b = Double(a) {
            dollarShow.text = "$\(b * 0.1367)"
        } else {
            dollarShow.text = "$0.00"
        }
    }
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        let currentText = RMBInput.text?.range(of: ".")
        let addingText = string.range(of: ".")
        
        if currentText != nil && addingText != nil {
            return false
        } else {
            return true
        }
    }
}
